<template>
  <div>
    <el-card shadow="never">
      <el-button icon="el-icon-document-add" type="primary" @click="handleAdd"
        >添加</el-button
      >
    </el-card>

    <el-table
      :data="tableData"
      border=""
      :header-cell-style="{ textAlign: 'center' }"
      :cell-style="{ textAlign: 'center' }"
    >
      <el-table-column label="Id">
        <template slot-scope="scope">
          <i class="el-icon-time"></i>
          <span>{{ scope.row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="文章类型">
        <template slot-scope="scope">
          <span>{{ scope.row.articleType }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否禁用">
        <template slot-scope="scope">
          <el-switch
            v-model="scope.row.isEnable"
            @change="change(scope.$index, scope.row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 对话框 -->
    <el-dialog title="文章类型" :visible.sync="dialogFormVisible" width="500px">
      <el-form :model="form">
        <el-form-item label="文章类型名称" :label-width="formLabelWidth">
          <el-input v-model="form.articleType" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addArticleType,
  getType,
  enableType,
  modArticle,
} from "../api/article";
export default {
  data() {
    return {
      dialogFormVisible: false,
      tableData: [],
      form: {
        id: 0,
        articleType: "",
        isEnable: true,
      },
      formLabelWidth: "120px",
      //当前下标
      nowIndex: 0,
    };
  },
  methods: {
    //编辑
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form.id = row.id;
      this.form.articleType = row.name;
      //获取下标
      this.nowIndex = index;
      console.log(index, row);
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
    //添加
    handleAdd() {
      this.dialogFormVisible = true;
      this.form.id = 0;
      this.form.articleType = "";
    },
    //确认
    confirm() {
      this.dialogFormVisible = false;
      console.log(this.form);
      if (this.form.id == 0) {
        if (this.form.articleType.length > 0) {
          addArticleType(this.form)
            .then(({ data }) => {
              if (data.code == 1000) {
                console.log(data);
                this.$message.error(data.msg);
              } else {
                console.log(data);
                this.tableData.push(data.data);
                this.$message.success(data.msg);
              }
            })
            .catch((err) => {
              console.log(err);
            });
        } else {
          this.$message({ type: "warning", message: "不能为空" });
        }
      } else {
        if (this.form.articleType.length > 0) {
          modArticle(this.form.id, this.form)
            .then(({ data }) => {
              this.tableData.splice(this.nowIndex, 1, data.data);
              this.$message.success(data.msg);
            })
            .catch((err) => {
              console.log(err);
            });
        }else{
          this.$message({ type: "warning", message: "不能为空" });
        }
      }
    },
    getList() {
      getType().then(({ data }) => {
        this.tableData = data.data;
      });
    },
    //修改状态
    change(index, row) {
      console.log(index, row);
      this.form.isEnable = row.isEnable;
      enableType(row.id, this.form).then(({ data }) => {
        console.log(data);
        if (data.data.isEnable == false) {
          this.$message.error(data.msg);
        } else {
          this.$message.success(data.msg);
        }
      });
    },
  },
  mounted() {
    this.getList();
  },
};
</script>

<style>
</style>